is default
ErrorsCollection

is default

Synthesised documentation from type/Routine type/Variable

From type/Routine

See Original text in context

multi sub trait_mod:<is>(Routine:D $r:$default!)

There is a special trait for Routines called is default. This trait is designed as a way to disambiguate multi calls that would normally throw an error because the compiler would not know which one to use. This means that given the following two Routines, the one with the is default trait will be called.

multi sub f() is default { say "Hello there" }
multi sub f() { say "Hello friend" }
f();   # OUTPUT: «"Hello there"␤»

The is default trait can become very useful for debugging and other uses but keep in mind that it will only resolve an ambiguous dispatch between two Routines of the same precedence. If one of the Routines is narrower than another, then that one will be called. For example:

multi sub f() is default { say "Hello there" }
multi sub f(:$greet{ say "Hello " ~ $greet }
f();   # "Use of uninitialized value $greet..." 

In this example, the multi without is default was called because it was actually narrower than the Sub with it.

From type/Variable

See Original text in context

Sets the default value with which a variable is initialized, and to which it is reset when Nil is assigned to it. Trait arguments are evaluated at compile time. Closures won't do what you expect: they are stored as is and need to be called by hand.

my Int $x is default(42);
say $x;     # OUTPUT: «42␤» 
$x = 5;
say $x;     # OUTPUT: «5␤» 
# explicit reset: 
$x = Nil;
say $x;     # OUTPUT: «42␤»

The trait is default can be used also with subscripting things like arrays and hashes:

my @array is default'N/A' );
@array[22].say;  # OUTPUT: N/A 
@array = Nil;
@array.say;      # OUTPUT: [N/A] 
@array[4].say;   # OUTPUT: N/A 
 
my %hash is default'no-value-here' );
%hash<non-existent-key>.say# OUTPUT: no-value-here 
%hash<foo> = 'bar';
%hash<>.say;                 # OUTPUT: {foo => bar} 
%hash<wrong-key>.say;        # OUTPUT: no-value-here